package com.miguo.plugins.alibaba;

import android.app.Activity;

import com.alibaba.sdk.android.AlibabaSDK;
import com.alibaba.sdk.android.trade.CartService;
import com.alibaba.sdk.android.trade.ItemService;
import com.alibaba.sdk.android.trade.callback.TradeProcessCallback;
import com.alibaba.sdk.android.trade.model.TradeResult;
import com.alibaba.sdk.android.webview.UiSettings;
import com.miguo.app.AppConfig;
import com.taobao.tae.sdk.model.TaokeParams;

/**
 * File Names: BaiChuan
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-10-28
 * Time: 10:24
 */
public class BaiChuan {

    /**
     * 商品详情
     *
     * @param activity
     * @param title     标题
     * @param item_id   商品ID
     * @param item_type 1. ItemType.TAOBAO 淘宝商品 2. ItemType.TMALL 天猫商品
     */
    public static void showTaokeItemDetailV2(Activity activity, String title, Long item_id, int item_type) {
        UiSettings settings = new UiSettings();
        settings.title = title;
        TaokeParams taokeParams = new TaokeParams();
        taokeParams.pid = AppConfig.TAOCONFIG.PID;
        AlibabaSDK.getService(ItemService.class).showTaokeItemDetailByItemId(activity, new TradeProcessCallback() {

            @Override
            public void onPaySuccess(TradeResult tradeResult) {
//                Log.i("AlibabaSDK-showTaokeItemDetailByItemId-支付成功");
            }

            @Override
            public void onFailure(int code, String msg) {
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Log.i("AlibabaSDK-showTaokeItemDetailByItemId-确认交易订单失败-code:" + code + "-msg:" + msg);
//                } else {
//                    Log.i("AlibabaSDK-showTaokeItemDetailByItemId-交易取消-code:" + code + "-msg:" + msg);
//                }
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Toast.makeText(activity, "AlibabaSDK-showTaokeItemDetailByItemId-确认交易订单失败-code:" + code + "-msg:" + msg, Toast.LENGTH_SHORT).show();
//                } else {
//                    Toast.makeText(activity, "AlibabaSDK-showTaokeItemDetailByItemId-交易取消-code:" + code + "-msg:" + msg, Toast.LENGTH_SHORT)
//                            .show();
//                }
            }
        }, settings, item_id, item_type, null, taokeParams);
    }

    /**
     * 购物车
     *
     * @param activity
     */
    public static void showCart(Activity activity) {

        AlibabaSDK.getService(CartService.class).showCart(activity, new TradeProcessCallback() {

            @Override
            public void onPaySuccess(TradeResult tradeResult) {
//                Log.i("AlibabaSDK-showCart-添加购物车成功");
            }

            @Override
            public void onFailure(int code, String msg) {
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Log.i("AlibabaSDK-showCart-打开购物车失败-code:" + code + "-msg:" + msg);
//                } else {
//                    Log.i("AlibabaSDK-showCart-取消购物车失败-code:" + code + "-msg:" + msg);
//                }
            }
        });
    }

    /**
     * 添加商品到购物车
     *
     * @param activity
     * @param itemId   商品ID
     */
    public static void addItem2Cart(Activity activity, String itemId) {
        TaokeParams taokeParams = new TaokeParams();
        taokeParams.pid = AppConfig.TAOCONFIG.PID;
        AlibabaSDK.getService(CartService.class).addTaoKeItem2Cart(activity, new TradeProcessCallback() {
            @Override
            public void onPaySuccess(TradeResult tradeResult) {
//                Log.i("AlibabaSDK-addItem2Cart-添加淘客商品到购物车成功");
            }

            @Override
            public void onFailure(int code, String msg) {
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Log.i("AlibabaSDK-addItem2Cart-添加淘客商品到购物车失败-code:" + code + "-msg:" + msg);
//                } else {
//                    Log.i("AlibabaSDK-addItem2Cart-添加淘客商品到购物车取消-code:" + code + "-msg:" + msg);
//                }
            }
        }, null, itemId, null, taokeParams);
    }


    public static void showPage(Activity activity, String title, String url) {
        UiSettings settings = new UiSettings();
        settings.title = title;
        AlibabaSDK.getService(ItemService.class).showPage(activity, new TradeProcessCallback() {

            @Override
            public void onPaySuccess(TradeResult tradeResult) {
//                Toast.makeText(activity,
//                        "支付成功" + tradeResult.paySuccessOrders + "   " + tradeResult.payFailedOrders, Toast.LENGTH_SHORT)
//                        .show();

            }

            @Override
            public void onFailure(int code, String msg) {
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Toast.makeText(activity, "AlibabaSDK-showPage-确认交易订单失败-code:" + code + "-msg:" + msg, Toast.LENGTH_SHORT).show();
//                } else {
//                    Toast.makeText(activity, "AlibabaSDK-showPage-交易取消-code:" + code + "-msg:" + msg, Toast.LENGTH_SHORT)
//                            .show();
//                }
            }
        }, null, url);
    }


    /**
     * 我的订单
     *
     * @param activity
     */
    public void showOrder(Activity activity) {
//        TaokeParams taokeParams = new TaokeParams();
//        taokeParams.pid = AppConfig.TAOCONFIG.PID;
//        AlibabaSDK.getService(OrderService.class).showTaokeOrder(activity, new TradeProcessCallback() {
//
//            @Override
//            public void onPaySuccess(TradeResult tradeResult) {
//                Log.i("AlibabaSDK-showOrder-支付成功");
//            }
//
//            @Override
//            public void onFailure(int code, String msg) {
//                if (code == ResultCode.QUERY_ORDER_RESULT_EXCEPTION.code) {
//                    Log.i("AlibabaSDK-showOrder-确认交易订单失败-code:" + code + "-msg:" + msg);
//                } else {
//                    Log.i("AlibabaSDK-showOrder-交易异常-code:" + code + "-msg:" + msg);
//                }
//            }
//        }, orderItem, taokeParams);
    }
}
